<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="login-left">
        <div class="brand-content">
          <h1>欢迎使用</h1>
          <p>您的专属管理系统</p>
        </div>
      </div>
      <div class="login-right">
        <div class="login-box">
          <div class="login-header">
            <h2>{{ isLogin ? '用户登录' : '用户注册' }}</h2>
            <p class="welcome-text">{{ isLogin ? '欢迎回来，请登录您的账号' : '创建新账号，开启您的旅程' }}</p>
          </div>
          <form @submit.prevent="isLogin ? handleLogin() : handleRegister()">
            <div class="form-group">
              <label for="username">用户名</label>
              <div class="input-wrapper">
                <input
                  type="text"
                  id="username"
                  v-model="form.username"
                  placeholder="请输入用户名"
                  required
                />
              </div>
            </div>
            <div class="form-group" v-if="!isLogin">
              <label for="realname">姓名</label>
              <div class="input-wrapper">
                <input
                  type="text"
                  id="realname"
                  v-model="form.realname"
                  placeholder="请输入姓名"
                  required
                />
              </div>
            </div>
            <div class="form-group" v-if="!isLogin">
              <label for="idCard">身份证号</label>
              <div class="input-wrapper">
                <input
                  type="text"
                  id="idCard"
                  v-model="form.idCard"
                  placeholder="身份证号"
                  required
                />
              </div>
            </div>
            <div class="form-group">
              <label for="userPassword">密码</label>
              <div class="input-wrapper">
                <input
                  type="password"
                  id="userPassword"
                  v-model="form.userPassword"
                  placeholder="请输入密码"
                  required
                />
              </div>
            </div>
            <div class="form-group" v-if="!isLogin">
              <label for="confirmPassword">确认密码</label>
              <div class="input-wrapper">
                <input
                  type="password"
                  id="confirmPassword"
                  v-model="form.confirmPassword"
                  placeholder="请再次输入密码"
                  required
                />
              </div>
            </div>
            <div class="form-options" v-if="isLogin">
              <label class="remember-me">
                <input type="checkbox" v-model="form.remember" />
                <span>记住我</span>
              </label>
              <a href="#" class="forgot-password">忘记密码？</a>
            </div>
            <button type="submit" class="login-btn">
              {{ isLogin ? '登录' : '注册' }}
            </button>
            <div class="register-link">
              {{ isLogin ? '还没有账号？' : '已有账号？' }}
              <a href="#" @click.prevent="toggleForm">{{ isLogin ? '立即注册' : '立即登录' }}</a>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      isLogin: true,
      form: {
        username: '',
        realname: '',
        idCard: '',
        userPassword: '',
        confirmPassword: '',
        remember: false
      }
    }
  },
  created() {
    
  },
  methods: {
    toggleForm() {
      this.isLogin = !this.isLogin
      // 重置表单
      this.form = {
        username: '',
        realname: '',
        idCard: '',
        userPassword: '',
        confirmPassword: '',
        remember: false
      }
    },
    handleLogin() {
      if (!this.form.username || !this.form.userPassword) {
        this.$message.error('请输入用户名和密码')
        return
      }
      // 登录逻辑
      this.$post('/login', this.form).then(res => {
        
        if (res.code == 200) {
          this.$message.success('登录成功')
          // 存储token
          localStorage.setItem('token', res.token)
          this.$store.commit('SET_USER_INFO', res.data)
          // 跳转到首页
          this.$router.push('/')
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        // this.$message.error('服务器出错啦~~')
        console.log(err)
      })
    },
    handleRegister() {
      // 这里添加注册逻辑
      if (this.form.userPassword !== this.form.confirmPassword) {
        this.$message.error('两次输入的密码不一致')
        return
      }

      this.$post('/register', this.form).then(res => {        
        if (res.code == 200) {
          this.$message.success('注册成功')
          this.toggleForm()
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        // this.$message.error('服务器出错了~~')
        console.log(err)
      })
    }
  }
}

</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f6f8fc 0%, #e9f0f7 100%);
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    radial-gradient(circle at 20% 20%, rgba(43, 88, 118, 0.05) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(78, 67, 118, 0.05) 0%, transparent 50%),
    url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="none" stroke="%23e9f0f7" stroke-width="0.5"/></svg>') repeat;
  opacity: 0.3;
  z-index: 0;
}

.login-wrapper {
  display: flex;
  width: 100%;
  max-width: 1000px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  position: relative;
  z-index: 1;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.login-wrapper:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.12);
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #2b5876 0%, #4e4376 100%);
  padding: 3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    radial-gradient(circle at 20% 20%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
    url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="none" stroke="white" stroke-width="0.5"/></svg>') repeat;
  opacity: 0.2;
}

.brand-content {
  text-align: center;
  position: relative;
  z-index: 1;
}

.brand-content h1 {
  font-size: 2.8rem;
  margin-bottom: 1rem;
  font-weight: 600;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.brand-content p {
  font-size: 1.3rem;
  opacity: 0.9;
  letter-spacing: 0.5px;
}

.login-right {
  flex: 1;
  padding: 3rem;
  background: rgba(255, 255, 255, 0.95);
  position: relative;
}

.login-right::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    radial-gradient(circle at 20% 20%, rgba(43, 88, 118, 0.03) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(78, 67, 118, 0.03) 0%, transparent 50%);
  z-index: 0;
}

.login-box {
  width: 100%;
  max-width: 100%;
  position: relative;
  z-index: 1;
}

.login-header {
  text-align: center;
  margin-bottom: 2.5rem;
}

h2 {
  color: #2b5876;
  margin-bottom: 0.5rem;
  font-size: 2rem;
  font-weight: 600;
}

.welcome-text {
  color: #666;
  font-size: 1rem;
  letter-spacing: 0.5px;
}

.form-group {
  margin-bottom: 1.8rem;
}

label {
  display: block;
  margin-bottom: 0.6rem;
  color: #2b5876;
  font-size: 0.95rem;
  font-weight: 500;
}

.input-wrapper {
  position: relative;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 0.9rem 1.2rem;
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: rgba(248, 249, 250, 0.8);
  -webkit-appearance: none;
  appearance: none;
}

input:focus {
  outline: none;
  border-color: #2b5876;
  box-shadow: 0 0 0 3px rgba(43, 88, 118, 0.1);
  background: white;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.8rem;
  font-size: 0.95rem;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #666;
}

.forgot-password {
  color: #2b5876;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #4e4376;
}

.login-btn {
  width: 100%;
  padding: 1rem;
  background: linear-gradient(135deg, #2b5876 0%, #4e4376 100%);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 1.1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 1.5rem;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  position: relative;
  overflow: hidden;
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: 0.5s;
}

.login-btn:hover::before {
  left: 100%;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(43, 88, 118, 0.2);
}

.login-btn:active {
  transform: translateY(0);
}

.register-link {
  text-align: center;
  color: #666;
  font-size: 0.95rem;
}

.register-link a {
  color: #2b5876;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.register-link a:hover {
  color: #4e4376;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .login-wrapper {
    flex-direction: column;
  }

  .login-left {
    padding: 2rem;
  }

  .login-right {
    padding: 2rem;
  }

  .brand-content h1 {
    font-size: 2.2rem;
  }

  .brand-content p {
    font-size: 1.1rem;
  }
}

/* 防止移动端输入框缩放 */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
  select:focus,
  textarea:focus,
  input:focus {
    font-size: 16px;
  }
}
</style> 